/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jwu2.core;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Sites models the DB of sites known to JWU2
 * @author Rolf
 */
public class Sites {
    private static Sites singleton;
    private Map<String, Site> sites;
    
    /**
     * Constructs a new Sites object
     */
    private Sites() {
        sites = new HashMap<String, Site>();
        
        loadDefaultSites();
    }
    
    /**
     * Loads the Sites singleton from disk if present, will create a new Sites
     * singleton if none is present to be loaded.
     * 
     * @return the loaded Sites singleton
     */
    private static Sites loadSingleton() {
        /*
         * TODO: Load the actual sites.dat
         */
        
        return new Sites();
    }

    /**
     * Returns the singleton object of Sites
     * @return the singleton object
     */
    public synchronized static Sites singleton() {
        if( singleton == null) {
            singleton = loadSingleton();
        }
        
        return singleton;
    }

    /**
     * Adds the default sites.
     */
    private void loadDefaultSites() {
        //wowace.com
        Site wowAce = new Site("wowace");
        wowAce.setFetchURL("http://www.wowace.com/addons/?page=%pageid%");
        wowAce.setDownloadURL("%url%");
        wowAce.setSiteURL("http://www.wowace.com");
        wowAce.setMultipleFetchUrl(true);
        wowAce.setBeginFetchID(1);
        wowAce.setStopOnDate(true);
        wowAce.setCreateDownloadURL(true);
        wowAce.setTwoStepDownloadURL(true);
        wowAce.setAddonDownloadInfoURL("http://www.wowace.com%addonid%files");
        wowAce.setDateHasMonthAndDaySwapped(true);
        wowAce.setDateHasTime(true);
        
        wowAce.addPattern(Site.Patterns.ADDONSTART, ".*<tr class=\".* row-joined-to-next\">.*");
        wowAce.addPattern(Site.Patterns.ADDONEND, ".*<tr class=\".* row-joined-to-next\">.*|</tbody>");
        wowAce.addPattern(Site.Patterns.NAME, ".*<td class=\"col-project\"><h2><a href=\".*\">(.*?)</a></h2></td>.*");
        wowAce.addPattern(Site.Patterns.AUTHOR, ".*class=\"user user-author\">(.*?)</a>.*");
        wowAce.addPattern(Site.Patterns.DESCRIPTION, "<td.*class=\"summary\">(.*?)</td>");
        wowAce.addPattern(Site.Patterns.DOWNLOADURL, "---N/A---");
        wowAce.addPattern(Site.Patterns.ENDMULTIPLEFETCH, ".*<a href=\".*\">(\\d+)</a>.*Next.*");
        wowAce.addPattern(Site.Patterns.ID, ".*<td class=\"col-project\"><h2><a href=\"(.*?)\">.*</a></h2></td>.*");
        wowAce.addPattern(Site.Patterns.ENDOFPAGE, "---N/A---");
        wowAce.addPattern(Site.Patterns.DATE, ".*<span class=\"standard-date\" title=\".*, (\\d+) (.+) (\\d+) (\\d+)\\:(\\d+)\\:\\d+.*");
        wowAce.addPattern(Site.Patterns.DOWNLOADPATTERN1, ".*<td class=\"col-file\"><a href=\"(.*?)\">.*</a></td>.*");
        wowAce.addPattern(Site.Patterns.DOWNLOADPATTERN2, ".*<a href=\"(.*?)\">Download</a>.*");

        //WowInterface
        Site wowInterface = new Site("WowInterface");
        wowInterface.setFetchURL("http://wowinterface.com/downloads/latest.php?sb=lastupdate&so=desc&sh=full&pt=f&page=%pageid%");
        wowInterface.setDownloadURL("http://wowinterface.com/downloads/download%url%");
        wowInterface.setMultipleFetchUrl(true);
        wowInterface.setBeginFetchID(1);
        wowInterface.setStopOnDate(true);
        wowInterface.setCreateDownloadURL(false);
        wowInterface.setDateHasTime(true);
        wowInterface.setDateIsAMPM(true);
                
        wowInterface.addPattern(Site.Patterns.ADDONSTART, "<td align=\"center\" class=\".*\" nowrap></td>");
        wowInterface.addPattern(Site.Patterns.ADDONEND, "</div></td>");
        wowInterface.addPattern(Site.Patterns.NAME, ".*href=\"fileinfo.*\">(.*?)</a>.*");
        wowInterface.addPattern(Site.Patterns.AUTHOR, ".*by.*\">(.*?)</a>");
        wowInterface.addPattern(Site.Patterns.DESCRIPTION, "---N/A---");
        wowInterface.addPattern(Site.Patterns.DOWNLOADURL, ".*href=\"fileinfo.php.*id=(.*?)\".*");
        wowInterface.addPattern(Site.Patterns.ENDMULTIPLEFETCH, ".*Page \\d+ of (\\d+).*");
        wowInterface.addPattern(Site.Patterns.ID, ".*href=\"fileinfo.php.*id=(.*?)\".*");
        wowInterface.addPattern(Site.Patterns.ENDOFPAGE, "---N/A---");
        wowInterface.addPattern(Site.Patterns.DATE, "(\\d+)\\-(\\d+)\\-(\\d+) <span class=\"time\">(\\d+)\\:(\\d+)\\s+(.*?)</span>");

        sites.put(wowInterface.getId(), wowInterface);
        sites.put(wowAce.getId(), wowAce);
    }
    
    /**
     * Quick access to a site with a given ID
     * @param id the id of the site to fetch
     * @return the site with the given id
     */
    public static Site getSiteFromID(String id) {
        return singleton().getSites().get(id);
    }
    
    /**
     * Get the set of ids for the currently registered sites
     * @return the set of ids
     */
    public static Set<String> getIDs() {
        return singleton().getSites().keySet();
    }
    
    /**
     * Get the map of sites
     * @return the map of sites
     */
    public Map<String, Site> getSites() {
        return sites;
    }
}
